<?php
function form_text($model, $attribute, $value = "", $options = array()) {
    $input = '<input type="text"%s />';
    
    $options["name"] = (empty($options["name"])) ? $model . "[$attribute]" : $options["name"];
    $options["value"] = $value;
    $options["id"] = (empty($options["id"])) ? $model . '_' . $attribute : $options["id"];
    $options["class"] = empty($options["class"]) ? "txt" : $options["class"];
    $attributes = "";
    
    foreach($options as $key => $value) {
        $attributes .= " $key=\"$value\"";
    }
    $input = sprintf($input, $attributes);
    
    echo $input;
}

function form_checkbox($model, $attribute, $value, $options = array()) {
  $input = '<input type="checkbox"%s />';
  
  $options["name"] = (empty($options["name"])) ? $model . "[$attribute]" : $options["name"];
  if($value == true) {
    $options["checked"] = 'checked';
  }
  $options["id"] = (empty($options["id"])) ? $model . '_' . $attribute : $options["id"];
  $options["class"] = empty($options["class"]) ? "chkbx" : $options["class"];
  $attributes = "";
  
  foreach($options as $key => $value) {
    $attributes .= " $key=\"$value\"";
  }
  $input = sprintf($input, $attributes);
  
  echo $input;
}

function form_textarea($model, $attribute, $textarea_value = "", $options = array()) {
    $input = '<textarea%s>%s</textarea>';
    
    $options["name"] = (empty($options["name"])) ? $model . "[$attribute]" : $options["name"];
    $options["id"] = (empty($options["id"])) ? $model . '_' . $attribute : $options["id"];
    $attributes = "";
    
    foreach($options as $key => $value) {
        $attributes .= " $key=\"$value\"";
    }
    $input = sprintf($input, $attributes, $textarea_value);
    
    echo $input;
}

function form_password($model, $attribute, $value = "", $options = array()) {
    $input = '<input type="password"%s />';
    
    $options["name"] = (empty($options["name"])) ? $model . "[$attribute]" : $options["name"];
    $options["value"] = $value;
    $options["id"] = (empty($options["id"])) ? $model . '_' . $attribute : $options["id"];
    $options["class"] = empty($options["class"]) ? "txt" : $options["class"];
    $attributes = "";
    
    foreach($options as $key => $value) {
        $attributes .= " $key=\"$value\"";
    }
    $input = sprintf($input, $attributes);
    
    echo $input;
}

function form_submit($model, $value = "", $options = array()) {
    $input = '<input type="submit"%s />';
    
    $options["name"] = (empty($options["name"])) ? $model . '[form_submit]' : $options["name"];
    $options["value"] = $value;
    $options["id"] = (empty($options["id"])) ? $model . '_form_submit' : $options["id"];
    $options["class"] = empty($options["class"]) ? "btn" : $options["class"];
    $attributes = "";
    
    foreach($options as $key => $value) {
        $attributes .= " $key=\"$value\"";
    }
    $input = sprintf($input, $attributes);
    
    echo $input;
}

function form_for($model, $action = "index", $options = array()) {
    $form = '<form%s>';
    
    $model_id = (empty($options["model_id"])) ? "" : $options["model_id"] . "/";
    $options["action"] = ROOT . "$model/$model_id$action" . (($options["to_script"] === false) ? "" : '.htm');
    $options["method"] = (empty($options["method"])) ? 'post' : $options["method"];
    $options["id"] = (empty($options["id"])) ? $action . '_' . $model . '_form' : $options["id"];
    if($options["multipart"] === true) {
      $options["enctype"] = 'multipart/form-data';
    }
    $attributes = "";
    
    foreach($options as $key => $value) {
        if($key != "to_script" and $key != "multipart" and $key != "model_id") {
            $attributes .= " $key=\"$value\"";
        }
    }
    
    $form = sprintf($form, $attributes);
    
    echo $form;
}

function form_hidden($model, $attribute, $value = "", $options = array()) {
    $input = '<input type="hidden"%s />';
    
    $options["name"] = (empty($options["name"])) ? $model . "[$attribute]" : $options["name"];
    $options["value"] = $value;
    $options["id"] = (empty($options["id"])) ? $model . '_' . $attribute : $options["id"];
    $attributes = "";
    
    foreach($options as $key => $value) {
        $attributes .= " $key=\"$value\"";
    }
    $input = sprintf($input, $attributes);
    
    echo $input;
}

function form_label($model, $attribute, $text, $options = array()) {
    $label = '<label for="%s"%s>%s</label>';
    
    $for = (empty($options["for"])) ? $model . '_' . $attribute : $options["for"];
    $attributes = "";
    
    foreach($options as $key => $value) {
        if($key == "for") {
            continue;
        }
        $attributes .= " $key=\"$value\"";
    }
    
    $label = sprintf($label, $for, $attributes, $text);
    
    echo $label;
}

function end_form_for() {
    $form = "</form>\n";
    echo $form;
}

function form_file($model, $attribute, $options = array()) {
    $input = '<input type="file"%s />';
    
    $options["name"] = (empty($options["name"])) ? $model . "[$attribute]" : $options["name"];
    $options["id"] = (empty($options["id"])) ? $model . '_' . $attribute : $options["id"];
    $options["class"] = empty($options["class"]) ? "file" : $options["class"];
    $attributes = "";
    
    foreach($options as $key => $value) {
        $attributes .= " $key=\"$value\"";
    }
    $input = sprintf($input, $attributes);
    
    echo $input;
}
?>